//
//  ViewController.m
//  数据存储
//
//  Created by song on 15/1/24.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

#define PATH  @"/Users/song/Desktop/person.data"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)plistclick:(id)sender {
    
    //把数据存入到Documents 里面，只能存储Fundation 框架的对象
    NSString *docpath =[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSArray *array =@[@"song",@"good"];
    NSDictionary *dic =@{@"name":@"song"};
    
    NSString *file =[docpath stringByAppendingString:@"/data.plist"];
    NSLog(@"%@",file);
    [array writeToFile:file atomically:YES];
    
    // 把数据存入到偏好设置里面，比较方便
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:@"hello" forKey:@"name"];
    [defaults setInteger:12 forKey:@"num"];
    
    [defaults synchronize];
    
    Person *p =[[Person alloc]init];
    p.name=@"song";
    p.age = 23;
    
    // 把对象归档，但是对象必须遵守 NScoding 协议
    [NSKeyedArchiver archiveRootObject:p toFile:PATH];
    
}

- (IBAction)preclick:(id)sender {
    
    // 把对象解档，
    Person *p =[NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
    NSLog(@"%@",p);
}
@end
